//
//  ItemsViewController.m
//  checkApp
//
//  Created by 小浩 on 2017/6/13.
//  Copyright © 2017年 ma. All rights reserved.
//

#import "ItemsViewController.h"
#import "ItemTableView.h"
#import "Scan_VC.h"

@interface ItemsViewController ()<tableViewEventDelegate>

@property (nonatomic,strong)ItemTableView *tableView;

@end

@implementation ItemsViewController

- (ItemTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[ItemTableView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height-49) style:UITableViewStylePlain];
        _tableView.eventDelegate = self;
        _tableView.isDrag = YES;
        _tableView.mj_footer = nil;
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"项目";
    
    [self.view addSubview:self.tableView];
    
    [self initData];
}

- (void)initData {
    NSDictionary *params = @{@"machineCode":@"loger168",
                             @"projectType":@"1"};
    [NetRequest postDataWithUrlString:ITEM_LIST_URL withParams:params success:^(id data) {
//        NSLog(@"data is %@",data);
        NSArray *array = data[@"data"];
        for (NSDictionary *dict in array) {
            ItemModel *model = [[ItemModel alloc] initWithDataDic:dict];
            [self.tableView.mArr addObject:model];
        }
        self.tableView.data = self.tableView.mArr;
        [self.tableView reloadData];
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSString *errorDes) {
        NSLog(@"数据请求失败");
//        [self hideBubble];
        [self showErrorWithTitle: @"网络有误" autoCloseTime: 2];
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView reloadData];
    } refresh:^{
        [self initData];
    }];
}

#pragma mark tableViewEventDelegate
//上拉
- (void)pullDown:(UITableView *)tableView {
    [self initData];
}
//选中
- (void)didSelectTableView:(UITableView *)tableView forIndexPath:(NSIndexPath *)indexPath {
    ItemModel *itemModel = [self.tableView.data objectAtIndex:indexPath.row];
    CGFloat timePP = [[NSDate date] timeIntervalSince1970];
    if (timePP < [itemModel.startTime floatValue]) {
        [self showErrorWithTitle: @"活动未开始" autoCloseTime: 2];
    }else if (timePP > [itemModel.endTime floatValue]) {
        [self showErrorWithTitle: @"活动已过期" autoCloseTime: 2];
    }else {
        Scan_VC *scanVC = [[Scan_VC alloc] init];
        scanVC.projectId = itemModel.itemId;
        scanVC.showInfoId = itemModel.showInfoId;
        scanVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:scanVC animated:YES];
    }
}

- (void)showFrameAnimationBubble:(UIButton *)sender {
    LKBubbleInfo *frameInfo = [[LKBubbleInfo alloc] init];
    NSMutableArray<UIImage *> *icons = [[NSMutableArray alloc] init];
    for (int i = 1 ; i <= 8; i ++) {
        [icons addObject: [UIImage imageNamed: [NSString stringWithFormat: @"lkbubble%d.jpg" , i]]];
    }
    frameInfo.iconArray = icons;
    // 在数组中依次放入多张图片即可实现多图循环播放
    frameInfo.backgroundColor = [UIColor colorWithRed: 238 / 255.0 green:238 / 255.0 blue:238 / 255.0 alpha:1];
    // 动画的帧动画播放间隔
    frameInfo.frameAnimationTime = 0.15;
    frameInfo.title = @"正在加载中...";
    frameInfo.titleColor = [UIColor darkGrayColor];
    [[LKBubbleView defaultBubbleView] showWithInfo: frameInfo];
}

- (void)hideBubbleFrame {
    [[LKBubbleView defaultBubbleView] hideBubble];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
